package com.freesun.shop.controller;

import com.freesun.shop.domin.ProdSolr;
import com.freesun.shop.entity.IndexImg;
import com.freesun.shop.entity.Notice;
import com.freesun.shop.entity.ProdTag;
import com.freesun.shop.service.IndexImgService;
import com.freesun.shop.service.NoticeService;
import com.freesun.shop.service.ProdTagService;
import com.freesun.shop.service.SearchService;
import com.freesun.shop.vo.IndexImgVo;
import com.freesun.shop.vo.NoticeVo;
import com.freesun.shop.vo.ProdTagVo;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class IndexController {

    @Reference (check = false)
    private IndexImgService indexImgService;

    @Reference (check = false)
    private NoticeService noticeService;

    @Reference (check = false)
    private ProdTagService prodTagService;

    @Reference (check = false)
    private SearchService searchService;

    @Autowired
    private MapperFacade mapperFacade; //快速复制对象属性

    @ApiOperation("微信小程序获取轮播图")
    @GetMapping("/indexImgs")
    public ResponseEntity<List<IndexImgVo>> loadIndexImag() {
        List<IndexImg> indexImgs=  indexImgService.getIndexImgs();
        List<IndexImgVo> indexImgVos = mapperFacade.mapAsList(indexImgs, IndexImgVo.class);
        return ResponseEntity.ok(indexImgVos);

    }

    @ApiOperation("加载置顶通知")
    @GetMapping("/shop/notice/topNoticeList")
    public ResponseEntity<List<NoticeVo>> loadTopNotices() {
        List<Notice> notices=  noticeService.getTopList();
        List<NoticeVo> noticeVos = mapperFacade.mapAsList(notices, NoticeVo.class);
        return ResponseEntity.ok(noticeVos);
    }

    @ApiOperation("加载商品标签")
    @GetMapping("/prod/tag/prodTagList")
    public ResponseEntity<List<ProdTagVo>> loadProdTags() {
      List<ProdTag> prodTags=  prodTagService.getProdTagList();
        List<ProdTagVo> prodTagsVo = mapperFacade.mapAsList(prodTags, ProdTagVo.class);
        return ResponseEntity.ok(prodTagsVo);
    }

    @ApiOperation("wx小程序通过商品标签查询商品")
    @GetMapping("/prod/prodListByTagId")
    public ResponseEntity<List<ProdSolr>> findByTagId(@RequestParam(required = true) Long tagId,
                                                      @RequestParam(defaultValue = "10")Integer size) {
        List<ProdSolr> prodSolrs= searchService.serachByTagId(tagId, size);
        return ResponseEntity.ok(prodSolrs);
    }
}
